package com.ror.java.se.thread;

//同步方法

public class Demo08 {
    public static void main(String[] args) {
        TicketConductor tc = new TicketConductor();
        Thread t1 = new Thread(tc, "窗口1");
        Thread t2 = new Thread(tc, "窗口2");
        Thread t3 = new Thread(tc, "窗口3");
        t1.start();
        t2.start();
        t3.start();
    }

    private static class TicketConductor implements Runnable {
        static int ticket = 0;

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (!sell()) break;
            }
        }

        private static synchronized boolean sell() {
            if (ticket >= 100) return false;
            ticket++;
            System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票！");
            return true;
        }
    }
}
